<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绑定数字令牌</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css" rel="stylesheet">
    <link href="/css/tips.css" rel="stylesheet">
</head>

<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <input type="hidden" name="userId" th:value="${userId}" />
                        <input type="hidden" name="secret" id="secret" th:value="${secret}"/>
                        <input type="hidden" id="code"  th:value="${code}"/>
                        <div class="form-group">
                            <label class="col-sm-5 control-label">1、使用微信扫描右侧二维码关注微信小程序</label>
                            <div class="col-sm-7">
                                <img src="/img/google.jpg" style="height: 300px">
                            </div>
                        </div>
                        <div class="form-group" style="margin-top: 100px;">
                            <label class="col-sm-5 control-label">2、点击加号扫描右侧二维码</label>
                            <div class="col-sm-7">
                                <div id="qrcode"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-5 control-label">3、输入小程序中对应的6位数字</label>
                            <div class="col-sm-7">
                                <input type="text" id="secretCode" name="code" class="form-control"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <button id="sub" type="submit" class="btn btn-primary">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script th:inline="javascript"> var ctx = [[@{/}]] ; </script>
<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>
<!--二维码生成器-->
<script src="/js/jquery.qrcode.min.js?v=1.0.0"></script>
<!-- 自定义js -->
<script src="/js/content.js?v=1.0.0"></script>

<script src="/js/ajax-util.js"></script>
<script src="/js/plugins/validate/jquery.validate.min.js" th:src="@{/js/plugins/validate/jquery.validate.min.js}"></script>
<script src="/js/plugins/validate/messages_zh.min.js" th:src="@{/js/plugins/validate/messages_zh.min.js}"></script>
<script src="/js/plugins/layer/layer.min.js" th:src="@{/js/plugins/layer/layer.min.js}"></script>
<script>
    $(document).ready(function () {
        //使用canvas生成
        $('#qrcode').qrcode({
            render: "canvas",
            text: $('#code').val()
        });
        $("#sub").on('click', function () {
            $("#signupForm").submit();
        });
        validateRule();

        $("body").keydown(keyDownLogon);
    })
    $.validator.setDefaults({
        submitHandler: function () {
            save();
        }
    });

    function keyDownLogon() {
        if (event.keyCode == "13") {
            $("#sub").trigger('click');
        }
    }

    function save() {
        $.ajax({
            type: "POST",
            url: ctx + "saveSecret",
            data: $('#signupForm').serialize(),
            success: function (r) {
                if (r.code == 0) {
                    var index = layer.load(1, {
                        shade: [0.1, '#fff'] //0.1透明度的白色背景
                    });
                    parent.location.href = '/index?secret=' + $('#secret').val();
                } else {
                    layer.msg(r.msg);
                }
            },
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                code: {
                    required: true
                }
            },
            messages: {
                code: {
                    required: icon + "请输入您的6位数动态密码",
                }
            }
        })
    }
</script>
</html>
